#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 添加
    ui->listWidget->addItem("C++");
    ui->listWidget->addItem("Java");
    ui->listWidget->addItem("Python");

    ui->listWidget->addItem(new QListWidgetItem("go"));
    ui->listWidget->addItem(new QListWidgetItem("汇编语言"));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_clicked()
{
    // 获取要插入的数据
    QString add = ui->lineEdit->text();
    // 插入数据
    ui->listWidget->addItem(add);
}

void Widget::on_pushButton_sub_clicked()
{
    // 获取要删除的行号
    int current = ui->listWidget->currentRow();
    // 判断是否合理
    if(current < 0)
        return;
    // 删除
    ui->listWidget->takeItem(current);
}

void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if(current != nullptr)
        qDebug() << "当前选中行:" << current->text();
    if(previous != nullptr)
        qDebug() << "上次选中行:" << previous->text();
}
